<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="110px">
      <el-form-item label="编号" prop="no">
        <el-input
          v-model="queryParams.no"
          placeholder="请输入编号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="客户名称" prop="customName">
        <el-input
          v-model="queryParams.customName"
          placeholder="请输入客户名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="业务员" prop="userName">
        <el-input
          v-model="queryParams.userName"
          placeholder="请输入业务员"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['custom:loan:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['custom:loan:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['custom:loan:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          :loading="exportLoading"
          @click="handleExport"
          v-hasPermi="['custom:loan:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="loanList" @selection-change="handleSelectionChange" border>
      <el-table-column type="selection" width="55" align="center" />
<!--      <el-table-column label="主键id" align="center" prop="id" />-->
      <el-table-column label="编号" align="center" prop="no" />
<!--      <el-table-column label="客户id" align="center" prop="customId" />-->
      <el-table-column label="客户编号" align="center" prop="customNo" />
      <el-table-column label="客户名称" align="center" prop="customName" />
      <el-table-column label="业务员" align="center" prop="userName" />
      <el-table-column label="放款类型" align="center" prop="loanType">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.custom_loan_type" :value="scope.row.loanType"/>
        </template>
      </el-table-column>
      <el-table-column label="放款金额" align="center" prop="loanMoney" />
      <el-table-column label="放款时间" align="center" prop="loanTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.loanTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="{row}">
<!--          <el-button-->
<!--            size="mini"-->
<!--            type="text"-->
<!--            icon="el-icon-view"-->
<!--            @click="handleDetail(row)"-->
<!--          >详情</el-button>-->
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(row)"
            v-hasPermi="['custom:loan:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(row)"
            v-hasPermi="['custom:loan:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <LoanModal ref="loanModal" @refresh="getList"/>
    <LoanDetail ref="loanDetail" />

  </div>
</template>

<script>
import { listLoan, delLoan, exportLoan } from "@/api/custom/loan";
import LoanModal from './modal';
import LoanDetail from './detail';

export default {
  name: "Loan",
  dicts: ['custom_loan_type'],
  components: { LoanModal, LoanDetail },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 客户放款表格数据
      loanList: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        no: null,
        customId: null,
        customNo: null,
        customName: null,
        userName: null,
        loanType: null,
        loanMoney: null,
        loanTime: null,
        orderByColumn: "id",
        isAsc: "desc"
      },
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询客户放款列表 */
    getList() {
      this.loading = true;
      listLoan(this.queryParams).then(response => {
        this.loanList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.$refs.loanModal.handleAdd();
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      const id = row.id || this.ids
      this.$refs.loanModal.handleUpdate(id);
    },
    /** 详情按钮操作 */
    handleDetail(row) {
      const id = row.id || this.ids
      this.$refs.loanDetail.handleDetail(id);
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除客户放款编号为"' + ids + '"的数据项？').then(function() {
        return delLoan(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$modal.confirm('是否确认导出所有客户放款数据项？').then(() => {
        this.download('custom/loan/export', {
        ...queryParams
        }, `客户放款_${new Date().getTime()}.xlsx`)
      }).catch(() => {});
    }
  }
};
</script>
